<?php
namespace Common\Model;
use Think\Model;

class SysconfigModel extends Model{


	protected $_validate = array(
		array('cfg_basehost','require','网站地址不能为空'),
	);

	protected $trueTableName = 'sysconfig';

	public function editReplaceWord(){
		$word = I('value');
		if($word == ''){
			$this->error='屏蔽字符不能为空';
			return false;
		}
		$data['varname'] = 'cfg_replace_word';
		$data['value'] = $word;
		$res = $this->save($data);
		if($res){
			return true;
		}else{
			$this->error="更新失败";
			return false;
		}
	}

	public function getReplaceWord(){
		$map['varname'] = 'cfg_replace_word';
		return $this->where($map)->find();
	}

	public function getSysConfig($group = 1){
		return $this->where(array('groupid'=>$group))->select();
	}

	public function editConfig(){
		$data = isset($_POST) ? $_POST : array();
		foreach($data as $k => $v){
			$data = array(
				'varname'	=> $k,
				'value' 	=> $v
			);
			$this->save($data);
		}
		$this->putOutConfig();
		return true;

	}

	public function putOutConfig(){
		$data = $this->field('varname,value')->select();
		$data = array_column($data,'value','varname');
		$path = SITE_PATH . "Application/Common/conf/sysconfig.php";
		file_put_contents($path, "<?php\treturn " . stripslashes(var_export($data, true)) . ";");
		return true;
	}
}
